package org.kjtc.mapper;

import org.apache.ibatis.annotations.*;
import org.kjtc.entity.*;

import java.util.List;

@Mapper
public interface WeChartMapper {

    @Select("select * from wechat.wechatmenu order by MenuID")
    List<WechatMenu> getWechatMenu();

    @Select("select MenuID, MenuName, MenuKey, ParentMenuID, Type, Url, Level, OrderNumber, MediaID, AppID, PagePath, Enabled from wechat.wechatmenu order by ParentMenuID, OrderNumber")
    List<WechatMenu> getWechatMenuAllLevel();

    @Select("select MenuID, MenuName, MenuKey, Type, Url, Level, OrderNumber, MediaID, AppID, PagePath, Enabled, ParentMenuID" +
            " from wechat.wechatmenu  where Level = #{Level} and ParentMenuID = #{ParentMenuID} and OrderNumber > 0" +
            " order by OrderNumber LIMIT #{LimitCount}")
    List<WechatMenu> getChangeMenuID(WechatMenu wechatMenu);

    @Select("select MenuID, MenuName, MenuKey, Type, Url, Level, OrderNumber, MediaID, AppID, PagePath, Enabled" +
            " from wechat.wechatmenu  where Level='button' order by OrderNumber LIMIT 3")
    List<WechatMenu> getParentMenuID(WechatMenu wechatMenu);

    @InsertProvider(type =WeChatProvider.class, method = "insertWechatMenu")
    int insertWechatMenu(WechatMenu wechatMenu);

    @InsertProvider(type =WeChatProvider.class, method = "saveMenuSure")
    int saveMenuSure(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateOrderNum")
    int updateOrderNum(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateWechatMenu")
    int updateWechatMenu(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateWeChatMenuSure")
    int updateWeChatMenuSure(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateMoveMenu")
    int updateMoveMenu(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateMoveMenuSure")
    int updateMoveMenuSure(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateMenuSure")
    int updateMenuSure(WechatMenu wechatMenu);

    @UpdateProvider(type =WeChatProvider.class, method = "updateWechatSure")
    int updateWechatSure(WechatMenu wechatMenu);

    @Delete("delete from wechat.wechatmenu where MenuID = #{menuID} or ParentMenuID = #{menuID}")
    int deleteWechatMenu(WechatMenu wechatMenu);

    @Insert("insert into wechat.wechatmessage (" +
            "SendFrom," +
            "Type," +
            "TemplateID," +
            "SendUser," +
            "Message," +
            "Data," +
            "Url," +
            "Appid," +
            "Pagepath," +
            "SendDTTM," +
            "SendResult," +
            "CreateUser," +
            "CreateDTTM," +
            "UpdateUser," +
            "UpdateDTTM" +
            ") values (" +
            "#{SendFrom}," +
            "#{Type}," +
            "#{TemplateID}," +
            "#{SendUser}," +
            "#{Message}," +
            "#{Data}," +
            "#{Url}," +
            "#{Appid}," +
            "#{Pagepath}," +
            "#{SendDTTM}," +
            "#{SendResult}," +
            "#{CreateUser}," +
            "SYSDATE()," +
            "#{UpdateUser}," +
            "SYSDATE()" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "ID")
    int insertWechatMessage(WechatMessage wechatMessage);

    @Update("update " +
            "wechat.wechatmessage " +
            "set " +
            "UpdateUser = #{UpdateUser}, " +
            "UpdateDTTM = SYSDATE(), " +
            "SendUser = #{SendUser}, " +
            "Data = #{Data}, " +
            "Message = #{Message}, " +
            "Url = #{Url}, " +
            "SendResult = #{SendResult} " +
            "where " +
            "ID = #{ID} ")
    int updateWechatMessage(WechatMessage wechatMessage);

//    @Select("select * from wechat.wechatmessage order by ID desc")
    @SelectProvider(type =WeChatProvider.class, method = "getWechatMessagePage")
    List<WechatMessage> getWechatMessagePage(WechatMessage wechatMessage);

    @Update("update " +
            "wechat.wechatuser " +
            "set " +
            "UpdateUser = #{UpdateUser}, " +
            "UpdateDTTM = SYSDATE(), " +
            "UserID = #{UserID}, " +
            "UserName = #{UserName}, " +
            "Mobile = #{Mobile}, " +
            "LoginEnabled = #{LoginEnabled} " +
            "where " +
            "WeChatID = #{WeChatID} ")
    int updateWechatUser(WechatUser wechatUser);

    @SelectProvider(type =WeChatProvider.class, method = "getWechatMessage")
    List<WechatMessage> getWechatMessage(WechatMessage wechatMessage);

    @SelectProvider(type =WeChatProvider.class, method = "getWechatUser")
    List<WechatUser> getWechatUser(WechatUser wechatUser);

    @SelectProvider(type =WeChatProvider.class, method = "getSendUser")
    List<WechatUser> getSendUser(WechatUser wechatUser);

    @Select("select WeChatNickName, UserID, UserName from wechat.wechatuser")
    List<WechatUser> getWechatUserName(WechatUser wechatUser);

    @Select("select count(*) from wechat.wechatuser where WeChatID = #{WeChatID}")
    int checkWechatUserOnly(WechatUser wechatUser);

    @Insert("insert into wechat.wechatuser (" +
            "UserID," +
            "UserName," +
            "Mobile," +
            "WeChatID," +
            "WeChatNickName," +
            "WeChatImageUrl," +
            "SubscribeTime," +
            "Enabled," +
            "LoginEnabled," +
            "CreateUser," +
            "CreateDTTM," +
            "UpdateUser," +
            "UpdateDTTM" +
            ") values (" +
            "#{UserID}," +
            "#{UserName}," +
            "#{Mobile}," +
            "#{WeChatID}," +
            "#{WeChatNickName}," +
            "#{WeChatImageUrl}," +
            "#{SubscribeTime}," +
            "1," +
            "0," +
            "#{CreateUser}," +
            "SYSDATE()," +
            "#{UpdateUser}," +
            "SYSDATE()" +
            ")")
    int insertWechatUser(WechatUser wechatUser);

    @UpdateProvider(type =WeChatProvider.class, method = "syncWechatUser")
    int syncWechatUser(WechatUser wechatUser);

    @Delete("delete from wechat.wechatuser where WeChatID in (#{weChatIDList})")
    int deleteWechatUser(String weChatIDList);

    @Select("select * from wechat.systemconfig order by CreateDTTM")
    List<SystemConfig> getSystemConfig();

    @Update("update " +
            "wechat.systemconfig " +
            "set " +
            "UpdateUser = #{UpdateUser}, " +
            "UpdateDTTM = SYSDATE(), " +
            "WeChatAppSecret = #{WeChatAppSecret}, " +
            "WeChatServiceUrl = #{WeChatServiceUrl}, " +
            "ModelUrl = #{ModelUrl}, " +
            "RedirectUrl = #{RedirectUrl}, " +
            "WeChatTimeout = #{WeChatTimeout} " +
            "where " +
            "WeChatAppID = #{WeChatAppID} ")
    int updateSystemConfig(SystemConfig systemConfig);

    @SelectProvider(type =WeChatProvider.class, method = "getWechatConfig")
    List<WechatConfig> getWechatConfig(WechatConfig wechatConfig);
}